﻿using System;
using System.Data;
using System.Data.Common;
using System.Text;
using Besture.Common;
using Besture.Model;

namespace Besture.Dal
{
    public partial class ColumnPropertyDal : BaseDataAccess
    {

        /// <summary>
        /// 获取详细信息
        /// </summary>
        /// <param name="id">主键值</param>
        /// <returns></returns>
        public ColumnPropertyModel GetModel(int id)
        {
            try
            {
                StringBuilder selectSql = new StringBuilder();
                selectSql.Append("SELECT * FROM TBColumnProperty");
                selectSql.Append(" WHERE ");
                selectSql.Append(" ID=@ID ");
                DbCommand ldcDBCommand = base.GetCommand(selectSql.ToString());
                base.SetParameter(ldcDBCommand, "@ID", DbType.Int32, id);
                DataTable dt = base.GetDataSet(ldcDBCommand).Tables[0];
                if (dt.Rows.Count > 0)
                    return ModelConvertHelper<ColumnPropertyModel>.ConvertToModelList(dt)[0];
                else return null;
            }
            catch (Exception exception)
            {
                WriteLog(exception.ToString());
                return null;
            }
        }

        /// <summary>
        /// 根据PartID获取数据
        /// </summary>
        /// <param name="partId">主键值</param>
        /// <returns></returns>
        public ColumnPropertyModel GetModelByPartId(int partId)
        {
            try
            {
                StringBuilder selectSql = new StringBuilder();
                selectSql.Append("SELECT * FROM TBColumnProperty");
                selectSql.Append(" WHERE ");
                selectSql.Append(" PartID=@PartID ");
                DbCommand ldcDBCommand = base.GetCommand(selectSql.ToString());
                base.SetParameter(ldcDBCommand, "@PartID", DbType.Int32, partId);
                DataTable dt = base.GetDataSet(ldcDBCommand).Tables[0];
                if (dt.Rows.Count > 0)
                    return ModelConvertHelper<ColumnPropertyModel>.ConvertToModelList(dt)[0];
                else return null;
            }
            catch (Exception exception)
            {
                WriteLog(exception.ToString());
                return null;
            }
        }

        /// <summary>
        /// 获取全部数据
        /// </summary>
        /// <returns></returns>
        public DataTable GetDataTable()
        {
            try
            {
                StringBuilder selectSql = new StringBuilder();
                selectSql.Append("SELECT * FROM TBColumnProperty ORDER BY ID DESC ");
                DbCommand ldcDBCommand = base.GetCommand(selectSql.ToString());
                DataTable dt = base.GetDataSet(ldcDBCommand).Tables[0];
                return dt;
            }
            catch (Exception exception)
            {
                WriteLog(exception.ToString());
                return null;
            }
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Add(ColumnPropertyModel model)
        {
            var identityId = -1;
            try
            {

				
            string sqlString = string.Format(@"
                INSERT INTO TBColumnProperty
                (
                        PartID
                        ,BelongsColumn
                        ,Type
                        ,Title
                        ,DeputyTitle
                        ,ImageUrl
                        ,ImageTips
                        ,LinkUrl
                        ,FileUrl
                        ,FileTips
                        ,ReleaseTime
                        ,Preface
                        ,Detail
                        ,Hits
                        ,IsDisplay
                        ,WebStick
                        ,ClassStick
                        ,Seo
                        ,CreateBy
                        ,CreateTime
                        ,UpdateBy
                        ,UpdateTime
                        ,PictureUrl
                        ,PictureTips
                )
                VALUES
                (
                        @PartID
                       ,@BelongsColumn
                       ,@Type
                       ,@Title
                       ,@DeputyTitle
                       ,@ImageUrl
                       ,@ImageTips
                       ,@LinkUrl
                       ,@FileUrl
                       ,@FileTips
                       ,@ReleaseTime
                       ,@Preface
                       ,@Detail
                       ,@Hits
                       ,@IsDisplay
                       ,@WebStick
                       ,@ClassStick
                       ,@Seo
                       ,@CreateBy
                       ,@CreateTime
                       ,@UpdateBy
                       ,@UpdateTime
                       ,@PictureUrl
                       ,@PictureTips
                )SELECT @@IDENTITY");

            DbCommand ldcDBCommand = base.GetCommand(sqlString);

            //参数设定
            SetParameter(ldcDBCommand, "@PartID", DbType.Int32, model.PartID);
            SetParameter(ldcDBCommand, "@BelongsColumn", DbType.Int32, model.BelongsColumn);
            SetParameter(ldcDBCommand, "@Type", DbType.Int32, model.Type);
            SetParameter(ldcDBCommand, "@Title", DbType.Int32, model.Title);
            SetParameter(ldcDBCommand, "@DeputyTitle", DbType.Int32, model.DeputyTitle);
            SetParameter(ldcDBCommand, "@ImageUrl", DbType.Int32, model.ImageUrl);
            SetParameter(ldcDBCommand, "@ImageTips", DbType.String, model.ImageTips);
            SetParameter(ldcDBCommand, "@LinkUrl", DbType.Int32, model.LinkUrl);
            SetParameter(ldcDBCommand, "@FileUrl", DbType.Int32, model.FileUrl);
            SetParameter(ldcDBCommand, "@FileTips", DbType.String, model.FileTips);
            SetParameter(ldcDBCommand, "@ReleaseTime", DbType.Int32, model.ReleaseTime);
            SetParameter(ldcDBCommand, "@Preface", DbType.Int32, model.Preface);
            SetParameter(ldcDBCommand, "@Detail", DbType.Int32, model.Detail);
            SetParameter(ldcDBCommand, "@Hits", DbType.Int32, model.Hits);
            SetParameter(ldcDBCommand, "@IsDisplay", DbType.Int32, model.IsDisplay);
            SetParameter(ldcDBCommand, "@WebStick", DbType.Int32, model.WebStick);
            SetParameter(ldcDBCommand, "@ClassStick", DbType.Int32, model.ClassStick);
            SetParameter(ldcDBCommand, "@Seo", DbType.Int32, model.Seo);
            SetParameter(ldcDBCommand, "@CreateBy", DbType.Int32, model.CreateBy);
            SetParameter(ldcDBCommand, "@CreateTime", DbType.DateTime, model.CreateTime);
            SetParameter(ldcDBCommand, "@UpdateBy", DbType.Int32, model.UpdateBy);
            SetParameter(ldcDBCommand, "@UpdateTime", DbType.DateTime, model.UpdateTime);

            SetParameter(ldcDBCommand, "@PictureUrl", DbType.Int32, model.PictureUrl);
            SetParameter(ldcDBCommand, "@PictureTips", DbType.String, model.PictureTips);

                identityId = base.ExecuteScalarInsert(ldcDBCommand);
            }
            catch (Exception exception)
            {
                WriteLog(exception.ToString());
            }
            return identityId;
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Update(ColumnPropertyModel model)
        {
            int iTmp = -1;
            try
            {
				
            string sqlString = string.Format(@"
                UPDATE TBColumnProperty
                SET
                    PartID = @PartID
                    ,BelongsColumn = @BelongsColumn
                    ,Type = @Type
                    ,Title = @Title
                    ,DeputyTitle = @DeputyTitle
                    ,ImageUrl = @ImageUrl
                    ,ImageTips = @ImageTips
                    ,LinkUrl = @LinkUrl
                    ,FileUrl = @FileUrl
                    ,FileTips = @FileTips
                    ,ReleaseTime = @ReleaseTime
                    ,Preface = @Preface
                    ,Detail = @Detail
                    ,Hits = @Hits
                    ,IsDisplay = @IsDisplay
                    ,WebStick = @WebStick
                    ,ClassStick = @ClassStick
                    ,Seo = @Seo
                    ,UpdateBy = @UpdateBy
                    ,UpdateTime = @UpdateTime
                    ,PictureUrl = @PictureUrl
                    ,PictureTips = @PictureTips
                WHERE
                    ID = @ID
            ");

            DbCommand ldcDBCommand = base.GetCommand(sqlString);
            //绑定SQL参数
            SetParameter(ldcDBCommand, "@ID", DbType.Int32, model.ID);
            SetParameter(ldcDBCommand, "@PartID", DbType.Int32, model.PartID);
            SetParameter(ldcDBCommand, "@BelongsColumn", DbType.Int32, model.BelongsColumn);
            SetParameter(ldcDBCommand, "@Type", DbType.Int32, model.Type);
            SetParameter(ldcDBCommand, "@Title", DbType.Int32, model.Title);
            SetParameter(ldcDBCommand, "@DeputyTitle", DbType.Int32, model.DeputyTitle);
            SetParameter(ldcDBCommand, "@ImageUrl", DbType.Int32, model.ImageUrl);
            SetParameter(ldcDBCommand, "@ImageTips", DbType.String, model.ImageTips);
            SetParameter(ldcDBCommand, "@LinkUrl", DbType.Int32, model.LinkUrl);
            SetParameter(ldcDBCommand, "@FileUrl", DbType.Int32, model.FileUrl);
            SetParameter(ldcDBCommand, "@FileTips", DbType.String, model.FileTips);
            SetParameter(ldcDBCommand, "@ReleaseTime", DbType.Int32, model.ReleaseTime);
            SetParameter(ldcDBCommand, "@Preface", DbType.Int32, model.Preface);
            SetParameter(ldcDBCommand, "@Detail", DbType.Int32, model.Detail);
            SetParameter(ldcDBCommand, "@Hits", DbType.Int32, model.Hits);
            SetParameter(ldcDBCommand, "@IsDisplay", DbType.Int32, model.IsDisplay);
            SetParameter(ldcDBCommand, "@WebStick", DbType.Int32, model.WebStick);
            SetParameter(ldcDBCommand, "@ClassStick", DbType.Int32, model.ClassStick);
            SetParameter(ldcDBCommand, "@Seo", DbType.Int32, model.Seo);
            SetParameter(ldcDBCommand, "@UpdateBy", DbType.Int32, model.UpdateBy);
            SetParameter(ldcDBCommand, "@UpdateTime", DbType.DateTime, model.UpdateTime);
            SetParameter(ldcDBCommand, "@PictureUrl", DbType.Int32, model.PictureUrl);
            SetParameter(ldcDBCommand, "@PictureTips", DbType.String, model.PictureTips);

                iTmp = base.ExecuteNonQuery(ldcDBCommand);
            }
            catch (Exception exception)
            {
                WriteLog(exception.ToString());
            }
            return iTmp;
        }

        /// <summary>
        /// 单个删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int Delete(int id)
        {
            int iTmp = -1;
            try
            {
                StringBuilder updateSql = new StringBuilder();
                updateSql.Append("DELETE FROM TBColumnProperty");
                updateSql.Append(" WHERE ");
                updateSql.Append(" ID=@ID ");
                DbCommand ldcDBCommand = base.GetCommand(updateSql.ToString());
                base.SetParameter(ldcDBCommand, "@ID", DbType.Int32, id);
                iTmp = base.ExecuteNonQuery(ldcDBCommand);
            }
            catch (Exception exception)
            {
                WriteLog(exception.ToString());
            }
            return iTmp;
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="idList"></param>
        /// <returns></returns>
        public int DeleteList(string ids)
        {
            int iTmp = -1;
            try
            {
                StringBuilder updateSql = new StringBuilder();
                updateSql.Append("exec('DELETE FROM TBColumnProperty");
                updateSql.Append(" WHERE ");
                updateSql.Append(" ID in ('+@ID+') ')");
                DbCommand ldcDBCommand = base.GetCommand(updateSql.ToString());
                base.SetParameter(ldcDBCommand, "@ID", DbType.String, ids);
                iTmp = base.ExecuteNonQuery(ldcDBCommand);
            }
            catch (Exception exception)
            {
                WriteLog(exception.ToString());
            }
            return iTmp;
        }
    }
}

